package com.smdk.dsminio.controller;


import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSONObject;
import com.smdk.dsminio.entity.DsminioUserInfo;
import com.smdk.dsminio.redis.RedisUtil;
import com.smdk.dsminio.request.BaseUtils;
import com.smdk.dsminio.service.DsminioUserInfoService;
import com.smdk.dsminio.utils.AjaxResult;
import com.smdk.dsminio.utils.GetIPAddress;
import com.smdk.dsminio.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Api(tags = {"用户登录"})
@RequestMapping("/api")
public class UserLoginController extends BaseUtils {

    @Autowired
    private DsminioUserInfoService dsminioUserInfoService;
    @Autowired
    private  RedisUtil REDISUTILS;
    @ApiOperation(value = "验证AccessToken")
    @GetMapping("verifyAccessToken")
    public AjaxResult<DsminioUserInfo> verify(String accessToken,HttpServletRequest request) {
        //验证token是否是同一个。不是则返回null
        try {
            Map<String, String> map = JwtUtil.decode(accessToken,GetIPAddress.getIp(request));
            String userId = map.get("userId");
            StaticLog.info("Token验证信息{}",map);
            String userInfo = REDISUTILS.get("userToken:"+userId);
            if (userInfo!=null){
                return AjaxResult.success(JSONObject.parseObject(userInfo,DsminioUserInfo.class));
            }else {
                return AjaxResult.fail("accessToken失效");
            }

        } catch (Exception e) {
            return AjaxResult.fail("认证失败");
        }
    }

    /**
     * 用户登录
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    @ApiOperation(value = "用户登录")
    public @ResponseBody
    AjaxResult userLogin(@ApiParam(value = "userName", required = true) @RequestParam(required = true, value = "userName") String userName,
                         @ApiParam(value = "password", required = true) @RequestParam(required = true, value = "password") String password,
                         HttpServletRequest request) {
        DsminioUserInfo userInfo = dsminioUserInfoService.userLogin(userName,password);
        if (userInfo!=null){
            String ip = GetIPAddress.getIp(request);
            String token = JwtUtil.encode(JSONObject.toJSONString(dsminioUserInfoService),ip);
            REDISUTILS.setex("userToken:"+userInfo.getUserId(), JSONObject.toJSONString(userInfo),86400);
            return AjaxResult.success(token,"登录成功");
        }else {
            return AjaxResult.fail("登录失败！用户名密码错误");
        }
    }


    /**
     * 退出登录
     *
     * @return String
     */
    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public AjaxResult logout(@ApiParam(value = "accessToken", required = true) @RequestParam String accessToken,HttpServletRequest request) {
       Map<String,String> jwtMap= JwtUtil.decode(accessToken,GetIPAddress.getIp(request));
       REDISUTILS.del("userToken:"+jwtMap.get("userId"));
       return AjaxResult.success("OK");
    }
}
